/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Velocidade.java
 *
 * Created on 18/05/2010, 17:35:04
 */

package br.leiam.eolica.graficos;


import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;

import sun.misc.BASE64Encoder;
import br.leiam.eolica.BD.ConsultaJfreeReady;
import br.leiam.eolica.excecoes.EolicaException;
import br.leiam.eolica.sistema.GerenciadorDeGraficos;

/**
 * 
 * @author Denis
 */
public class Velocidade extends javax.swing.JPanel {
	private org.jfree.data.jdbc.JDBCXYDataset chartDataset;
	private JFreeChart chart;
	private ConsultaJfreeReady consulta;

	private boolean flag = false;

	/**
	 * Creates new form Velocidade
	 * 
	 * @throws Exception
	 */
	public Velocidade(String tabela, String DataInicio, String DataFim,
			int altura, int tamX, int tamY) throws EolicaException {
		super();
		this.setSize(tamX, tamY);
		initComponents();

		ChartPanel chartPanel = calcularChart(tabela, DataInicio, DataFim,
				altura);
		jPanel1.add(chartPanel);
		chartPanel.setSize(tamX, tamY);
		
		this.setSize(tamX, tamY);
		jPanel1.setSize(tamX, tamY);
		// this.repaint();
		flag = true;

	}

	private ChartPanel calcularChart(String tabela, String DataInicio,
			String DataFim, int altura) throws EolicaException {		
		
		//String coluna = GerenciadorDeGraficos.getAltura(altura);
		String coluna = getColuna(altura);
		
		
		
		
		String query = "select datahora as 'Data'," + coluna + "/10"// ADICIONADO PARA FAZER A CONVERSAO PARA M/S(PROF. ALEXANDRE)
				+ " as velocidade from " + tabela
				+ " where datahora between timestamp('" + DataInicio
				+ "') and timestamp('" + DataFim + "')";

		consulta = new ConsultaJfreeReady(query);
		chartDataset = consulta.getChartDataset();
		chart = org.jfree.chart.ChartFactory.createTimeSeriesChart(
				"Velocidade a " + altura +"m", "Tempo", " Velocidade (m/s)",
				chartDataset, false, true, true);
		XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.gray);
		ChartPanel chartPanel = new ChartPanel(chart);
		return chartPanel;
	}
	
	public static String getColuna(int altura) throws EolicaException {// FIXME Qual coluna retornar? Media, Max, Min ou desvio padrao??
		
		if (altura == 102) {
			return "s1a";
		} else if (altura == 100) {
			return "s2a";
		} else if (altura == 70) {
			return "s3a";
		} else  {
			return "p1a";
		} 
	}

	
	
	

	public String getBase64String() {
		String figura = "";
		this.setVisible(true);

		try {
			double escalaX = 1, escalaY = 1;
			Component co = this;
			BufferedImage bi = new BufferedImage(
					(int) (co.getSize().getWidth() * escalaX), (int) (co
							.getSize().getHeight() * escalaY),
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = bi.createGraphics();
			g2d.scale(escalaX, escalaY);
			co.paint(g2d);

			byte[] figBytes;

			figBytes = ChartUtilities.encodeAsPNG(bi);

			BASE64Encoder encoder = new BASE64Encoder();
			figura = encoder.encode(figBytes);
		} catch (Exception ex) {
			Logger.getLogger(Weibull.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return figura;
	}

	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();

		setMinimumSize(new java.awt.Dimension(500, 400));

		jPanel1.setLayout(new java.awt.BorderLayout());

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 500,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400,
				Short.MAX_VALUE));

		getAccessibleContext().setAccessibleParent(this);
	}

	private javax.swing.JPanel jPanel1;

	/**
	 * @return the flag
	 */
	public boolean isFlag() {
		return flag;
	}

	/**
	 * @param flag
	 *            the flag to set
	 */
	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}
